package com.itools.core.controller;

import com.itools.core.base.CommonResult;
import com.itools.core.log.Logable;
import com.itools.core.result.FmsFileRecordPathResult;
import com.itools.core.result.FmsFileUploadResult;
import com.itools.core.result.FmsMultipartFileResult;
import com.itools.core.service.FmsFileHandleStrategyService;
import com.itools.core.validate.ParamsValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @project: itools-backend
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-05 14:43
 */
@RestController
@Api("文件系统处理策略服务接口")
public class FmsFileHandleStrategyController {
    @Autowired
    private FmsFileHandleStrategyService fmsFileHandleStrategyService;

    /**
     * 单个文件上传
     * 返回文件的唯一ID
     * @param file
     * @param uploadToken
     * @return
     */
    @RequestMapping(value = "/upload/file",method = RequestMethod.POST)
    @Logable
    @ApiOperation(value = "文件上传", notes = "文件上传", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadToken", value = "文件上传token", required = true, dataType = "String", paramType = "query")
    })
    @ParamsValidate(argsIndexs = {0,1})
    public CommonResult<FmsFileUploadResult> singletonUploadFile(@NotNull(message = "FMSCODE03") MultipartFile file,
                                                                 @NotBlank(message = "FMSCODE15") String uploadToken) {
        return fmsFileHandleStrategyService.singletonUploadFile(file,uploadToken);
    }

    /**
     * 返回文件对应的唯一ID
     * 文件批量上传
     * @param files 文件
     * @param uploadToken 申请的token
     * @return
     */
    @PostMapping("/multipart/upload/file")
    @ApiOperation(value = "批量上传文件", notes = "批量上传文件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadToken", value = "文件上传token", required = true, dataType = "String", paramType = "query")
    })
    public CommonResult<List<FmsMultipartFileResult>> filesMultipartUpload(@RequestParam("files") MultipartFile[] files,
                                                                           String uploadToken) {
        return fmsFileHandleStrategyService.filesMultipartUpload(files,uploadToken);
    }

    /**
     * 预览文件
     * 获取文件访问路径
     * @param uniqueIds
     * @param expiredTime
     * @param maxAccessCount
     * @param type
     * @return
     */
    @PostMapping("/get/files/path")
    @Logable
    @ApiOperation(value = "获取文件访问路径", notes = "获取文件访问路径", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uniqueIds", value = "文件的唯一IDS数组", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "expiredTime", value = "有效时长，单位分钟", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "maxAccessCount", value = "最大访问次数", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "文件下载 download/展示 show", required = true, dataType = "String", paramType = "query")
    })
    public CommonResult<List<FmsFileRecordPathResult>> getFileAccessUrl(@RequestParam(value = "uniqueIds",required = false) List<String> uniqueIds,
                                                                        @RequestParam(value = "expiredTime",required = false) Integer expiredTime,
                                                                        @RequestParam(value = "maxAccessCount",defaultValue = "-1", required = false) Integer maxAccessCount,
                                                                        @RequestParam(value = "type",required = false) String type) {
        return fmsFileHandleStrategyService.getFileUrlByUniqueIds(uniqueIds, expiredTime, maxAccessCount, type);
    }
    /**
     * 下载文件
     * @param uniqueId 文件唯一id
     * @param fileAccessToken 文件的访问token
     * @param response
     */
    @ApiOperation(value = "下载文件", notes = "下载文件", httpMethod = "GET")
    @PostMapping("/download/file")
    @Logable
    public void downloadFile(@RequestParam("uniqueId") String uniqueId,
                             @RequestParam("fileAccessToken") String fileAccessToken,
                             HttpServletResponse response) {
        fmsFileHandleStrategyService.downloadFile(fileAccessToken,response,uniqueId);
    }
}
